package com.yuejie.musicai.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.yuejie.musicai.beans.Result;
import com.yuejie.musicai.services.impl.ResultServiceImpl;

/**
 * 搜索结果相关Controller
 * @author Ocelot
 *
 */
@Controller
public class ResultController {
	
	@Autowired
	ResultServiceImpl resultservice;
	
	private static final Logger logger = LoggerFactory.getLogger(ResultController.class);

	/**
	 * 返回搜索结果页面
	 * @param qword
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/result")
	public String result(@RequestParam String qword, @RequestParam String page, Model model) {
		return resultview(model, qword, page);
	}
	
	/**
	 * 返回搜索结果页面
	 * @param qword
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/result/{qword}/{page}")
	public String resultrest(@PathVariable String qword, @PathVariable String page, Model model) {
		return resultview(model,  qword, page);
	}

	/**
	 * 在Model中设置页面参数
	 * @param model
	 * @param qword
	 * @param page
	 * @return
	 */
	private String resultview(Model model, String qword, String page) {
		logger.info("nothing!");
		model.addAttribute("qword", qword);
		model.addAttribute("pageno", page);
		Result rs = resultservice.getResult(qword, page);
		model.addAttribute("rs", rs);
		model.addAttribute("pagelink", resultservice.getPagelink(Integer.parseInt(page), Integer.parseInt(rs.getLength()) / 20, qword));
		return "result";
	}

}
